package com.lemon.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lemon.domain.*;
import com.lemon.domain.dto.SkuDto;
import com.lemon.domain.dto.WarehousingDto;
import com.lemon.service.WarehousingEntryDetailsService;
import com.lemon.service.WarehousingEntryDetailsSortService;
import com.lemon.service.WarehousingEntryService;
import com.lemon.service.WarehousingEntryUpService;
import com.lemon.service.impl.WarehousingEntryServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
* 入库单表(warehousing_entry)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/warehousing_entry")
@RequiredArgsConstructor
public class WarehousingEntryController {
/**
* 服务对象
*/
    private final WarehousingEntryService warehousingEntryService;
    private final WarehousingEntryDetailsService warehousingEntryDetailsService;
    private final WarehousingEntryDetailsSortService warehousingEntryDetailsSortService;
    private final WarehousingEntryUpService warehousingEntryUpService;

    @GetMapping("/{id}")
    public Result getEntry(@PathVariable Integer id){
        List<SkuDto> list  = warehousingEntryDetailsService.getEntry(id);
        return Result.success(list);
    }

    @GetMapping("/list")
    public Result getEntryList(){
        return Result.success(warehousingEntryService.list());
    }

    @GetMapping
    public Result getEntryPage(Integer page, Integer pageSize){
        PageResult pageResult = warehousingEntryService.getPage(page, pageSize);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addEntry(@RequestBody WarehousingDto dto){
        warehousingEntryService.add(dto);
        return Result.success();
    }

    @PutMapping
    @Transactional
    public Result updateEntry(@RequestBody WarehousingDto dto){
        deleteEntry(dto.getId());
        dto.setId(null);
        addEntry(dto);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Transactional
    public Result deleteEntry(@PathVariable Integer id){
        List<WarehousingEntryDetails> details = warehousingEntryDetailsService.getList(id);
        if (CollUtil.isNotEmpty( details)){
            details.forEach(detail->{
                warehousingEntryDetailsService.remove(Wrappers.<WarehousingEntryDetails>lambdaQuery().eq(WarehousingEntryDetails::getWarehousingEntryId,id));
                warehousingEntryDetailsSortService.remove(Wrappers.<WarehousingEntryDetailsSort>lambdaQuery().eq(WarehousingEntryDetailsSort::getWarehousingEntryDetailsId,detail.getId()));
                warehousingEntryUpService.remove(Wrappers.<WarehousingEntryUp>lambdaQuery().eq(WarehousingEntryUp::getEntryDetailsId,id));
            });
        }

        return warehousingEntryService.removeById(id) ? Result.success() : Result.error("删除失败");
    }

}
